package com.ifar.audiosdk.model.main.viewmodel

import androidx.lifecycle.MutableLiveData
import com.ifar.audiosdk.api.ApiRepository
import com.ifar.audiosdk.base.IfarBaseViewModel
import com.ifar.audiosdk.model.main.model.CheckUserCallStateEntity
import com.ifar.audiosdk.model.main.model.UpdateUsersCallStateEntity
import com.ifar.audiosdk.model.main.model.UserListEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class MainViewModel : IfarBaseViewModel() {
    private val repository by lazy { ApiRepository() }
    val apiError: MutableLiveData<String> = MutableLiveData()
    val apiGetUserListSuccess: MutableLiveData<UserListEntity> = MutableLiveData()
    val apiUpdateUsersCallState: MutableLiveData<UpdateUsersCallStateEntity> = MutableLiveData()
    val apiCheckUserCallState: MutableLiveData<CheckUserCallStateEntity> = MutableLiveData()

    /**
     * 获取用户列表
     */
    fun getUserList() {
        launch {
            val resp = withContext(Dispatchers.IO) { repository.getUserList() }
            executeResponse(resp, {
                apiGetUserListSuccess.value = resp.data
            }, {
                apiError.value = resp.msg
            })
        }
    }

    /**
     * 更新用户通话状态，可多个
     */
    fun updateUsersCallState(userIDs: String, channel_id: String) {
        launch {
            val resp =
                withContext(Dispatchers.IO) { repository.updateUsersCallState(userIDs, channel_id) }
            executeResponse(resp, {
                apiUpdateUsersCallState.value = resp.data
            }, {
                apiError.value = resp.msg
            })
        }
    }

    /**
     * 检测用户通话状态，可多个
     */
    fun checkUserCallState(userIDs: String) {
        launch {
            val resp = withContext(Dispatchers.IO) { repository.checkUserCallState(userIDs) }
            executeResponse(resp, {
                apiCheckUserCallState.value = resp.data
            }, {
                apiError.value = resp.msg
            })
        }
    }
}